// pages/withdrawalDetails/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    type:1,
    page:1,
    pageTotal:0,
    checked: false,
    list:[],
    productIds:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.hideShareMenu()
    let that = this
    
    var pages = getCurrentPages(); 
    var prevPage = pages[pages.length - 2];   //上一页
    console.log('prevPage',prevPage.data.lists)
    let productIds = []
    prevPage.data.lists.forEach((item,index)=>{
      productIds.push(item.id)
    })
    this.setData({
      productIds:productIds
    }, () => { 
      this.storeDeskProList()
   })
    
  },
  storeDeskProList(){
    var that=this
    let data = {
      storeId:wx.getStorageSync('userInfo').storeId
    }
    app.agriknow.storeDeskProList(data).then(res=>{
      console.log(res)
      if(res.code=='000000'){
        let list = res.data
        list.forEach((item,index)=>{
          
          var productIds=this.data.productIds
          console.log(productIds)
          var idsIndex=productIds.findIndex(ite=> item.id==ite)
          console.log(idsIndex)
          if(idsIndex>-1){
            item.checked=true
          }else{
            item.checked=false
          }
          
        })
        that.setData({
          list:list
        })
      }else{
        this.tipsMessage(res.data)
      }
    })
  },
  tipClick(e){
    console.log(e)
    let index = e.currentTarget.dataset.index
    this.setData({
      tabIndex:index
    })
  },
  onCheckbox(event) {
    console.log(event)
    let list = this.data.list
    let index = event.currentTarget.dataset.index
    list[index].checked = !list[index].checked
    this.setData({
      list: list,
    });
  },
  submit(){
    let list = []
    this.data.list.forEach((item,index)=>{
      if(item.checked == true){
        list.push(item)
      }
    })
    var that = this;
    var pages = getCurrentPages(); 
    var prevPage = pages[pages.length - 2];   //上一页
    prevPage.setData({
      lists:list
    })
    console.log(list)

    wx.navigateBack({
      delta: 1
    })
  },
  //触底加载
  scrollbottom() {
    console.log(1)
    if(this.data.pageTotal > this.data.list.length){
      this.setData({
        page: this.data.page + 1,
      });
    }else{
      this.tipsMessage('没有更多了！')
    }
  },
  //提示信息
  tipsMessage(text) {
    wx.showToast({
      title: text,
      icon: 'none',
      duration: 2000,
      mask: true
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})